<?php

class TagAction extends HomeCommonAction {
	
    public function index(){
    	$tags = $this->getTags();
    	$shares = $this->getShares();
    	
    	$this->assign("shares",$shares);
    	$this->assign("tags",$tags);
    	
    	$this->display();
    }
    
    public function tagShareList(){
    	$map['subject'] = $_REQUEST['subject'];
    	$map['status'] = 1;
    	$subject = $_REQUEST['subject'];
    	$order = " s.count_view desc ";
    	
    	$count = M("Tag")->where ( $map  )->count ( 'id' );
//		dump(M("Tag")->getlastsql());
    	if ($count > 0) {
			import ( "ORG.Util.Page" );
			$listRows = '10';
			$p = new Page ( $count, $listRows );
			$limit = $p->firstRow . ',' . $p->listRows;
	    	$sql = "select * from cm_tag as t left join cm_share as s on t.share_id=s.id where t.subject = '".$map['subject']."' and t.status=1 order by $order limit $limit";
	    	$list = M("")->query($sql);
//	    	echo M("")->getLastSql();
    		//分页时保证跳转结构
			foreach ( $map as $key => $val ) {
				if (! is_array ( $val ) && $key != '_string') {
					$p->parameter .= "$key=" . urlencode ( $val ) . "&";
				}
			}
			$page = $p->show();
    	}
    	foreach($list as $k=>$v){
    		$list[$k]['commodity'] = $this->getInfo(M("BaseCommodity"), $v['commodity_id']);
    		$list[$k]['brand'] = $this->getInfo(M("BaseBrand"), $v['brand_id']);
    	}
    	$list = $this->_formatList($list);
//    	dump($list);
    	$this->assign("list", $list);
    	$this->assign("page", $page);
		$this->display("tag_share_list");
    	
    }
	
	public function add(){
		$data['subject'] = $_REQUEST["subject"];
		$data['share_id'] = $_REQUEST["share_id"];
		$data['uid'] = Session::getLocal('id');
		$data['create_time'] = time();
		$data['status'] = 1;
		$result = M("Tag")->data($data)->add();
		return $result;
	}
	
	
	
    //获得标签
    public function getTags(){
    	$map_user = array(
    		"uid"      => Session::getLocal('id')
    	);
    	$tags = $this->getListField(M("Tag"),$map_user,'subject, count(subject) as num','num desc','',"subject");
    	
    	return $tags;
    }
    
    public function getShares(){
    	if($_REQUEST['subject']) $map['subject'] = $_REQUEST['subject'];
    	$map['status'] = 1;
    	$map['uid'] = Session::getLocal('id');
    	$order = "create_time desc";
    	$list = $this->getList(M("Tag"), $map, $order, "10");
    	foreach($list as $k=>$v){
    		$list[$k]['share'] = $this->getInfo(M("Share"),$v['share_id']);
    		$list[$k]["info"] = $this->getInfo(M("BaseCommodity"),$list[$k]['share']['commodity_id']);
    		$list[$k]['brand_info'] = $this->getInfo(M("BaseBrand"), $list[$k]["info"]['brand_id']);
    		$list[$k]['category_info'] = $this->getInfo(M("BaseCategory"), $list[$k]["info"]['category_id']);
    		$maps = array(
    					"uid" => Session::getLocal('id'), 
    					"status" => 1, 
    					"share_id" => $v['share_id']
    		);
    		$list[$k]['tags'] = $this->getListField(M("Tag"), $maps, "subject");
    	}
    	return $list;
    	
    }
    
    
    /**
     * [内部调用/数据处理]
     * 
     */
    public function _formatList($list){
    	if(is_array($list)){
    		foreach($list as $k=>$v){
    			$list[$k]["content"] = msubstr($v['content'], 0,200);
    		}
    	}
    	return $list;
    }
    
    
    
}
?>

